"""
====================================================================
Copyright (C) 2012-2013 Eric Blond

This file is part of Neptune.

Neptune is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Neptune is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Neptune.  If not, see <http://www.gnu.org/licenses/>.
====================================================================

Created on Dec 10, 2012

@author: Eric
"""

import wx
from ..nucleus import public as nucleus
from . import utils

#######

class MenuItem(nucleus.Page):

    _WxMenuItem = None
    _Context    = None

    Text  = None
    Help  = None
    _Kind = wx.ITEM_NORMAL
    
    def _on_IsEnabled_change(self, value):
        self._WxMenuItem.Enable(bool(value))

    @nucleus.cell(settable = True, on_change = '_on_IsEnabled_change')
    def IsEnabled(self):
        return True

    OnClick = None

    def _wx_on_menu_event(self, event):
        utils.call_user_cb((~self).OnClick)
        
    def _create(self, context):
        self._Context = context
        self._WxMenuItem = wx.MenuItem(context._WxMenu,
                                       wx.ID_ANY,
                                       unicode(self.Text if self.Text is not None else ''),
                                       unicode(self.Help if self.Text is not None else ''),
                                       self._Kind)
        if self.OnClick:
            context._WxFrame.Bind(wx.EVT_MENU,
                                  self._wx_on_menu_event,
                                  self._WxMenuItem)
        if not self.IsEnabled:
            self._on_IsEnabled_change(self.IsEnabled)
    
    def _destroy(self):
        self._WxMenuItem.Destroy()
        self._WxMenuItem = None
        self._Context = None

#######

class CheckMenuItem(nucleus.Page):

    Text = None
    Help = None
    OnClick = None
    
    def _on_click(self, *args):
        self.IsChecked = self._MenuItem._WxMenuItem.IsChecked()
        utils.call_user_cb((~self).OnClick)
        
    @nucleus.cell
    def _MenuItem(self):
        return MenuItem(Text = (~self).Text,
                        Help = (~self).Help,
                        _Kind   = wx.ITEM_CHECK,
                        OnClick = self._on_click)
    
    @nucleus.cell
    def _WxMenuItem(self):
        return self._MenuItem._WxMenuItem

    def _on_IsChecked_change(self, value):
        self._MenuItem._WxMenuItem.Check(bool(value))

    @nucleus.cell(settable = True, on_change = '_on_IsChecked_change')
    def IsChecked(self):
        return False

    def _create(self, context):
        self._MenuItem._create(context)
        if self.IsChecked:
            self._on_IsChecked_change(True)

    def _destroy(self):
        self._MenuItem._destroy()
